<template>
<div class="feedback">
    <div class="formTitle">是时候表达点什么了~~~<span @click="feedbackClose"><Icon type="ios-close feedbackClose"></Icon></span></div>
    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="50">
        <FormItem label="姓名" prop="name">
            <Input v-model="formValidate.name" placeholder="Enter your name"></Input>
        </FormItem>
        <FormItem label="邮箱" prop="mail">
            <Input v-model="formValidate.mail" placeholder="Enter your e-mail"></Input>
        </FormItem>
        <FormItem label="性别" prop="gender">
            <RadioGroup v-model="formValidate.gender">
                <Radio label="male">Male</Radio>
                <Radio label="female">Female</Radio>
            </RadioGroup>
        </FormItem>
        <FormItem label="爱好" prop="interest">
            <CheckboxGroup v-model="formValidate.interest">
                <Checkbox label="Eat"></Checkbox>
                <Checkbox label="Sleep"></Checkbox>
                <Checkbox label="Run"></Checkbox>
                <Checkbox label="Movie"></Checkbox>
            </CheckboxGroup>
        </FormItem>
        <FormItem label="内容" prop="desc">
            <Input v-model="formValidate.desc" type="textarea" :autosize="{minRows: 2,maxRows: 4}" placeholder="Enter something..."></Input>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit('formValidate')">Submit</Button>
            <Button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">Reset</Button>
        </FormItem>
    </Form>
</div>
</template>
<script>
    export default {
        name: 'feedback',
        data () {
            return {
                formValidate: {
                    name: '',
                    mail: '',
                    gender: '',
                    interest: [],
                    desc: ''
                },
                ruleValidate: {
                    name: [
                        { required: true, message: 'The name cannot be empty', trigger: 'blur' }
                    ],
                    mail: [
                        { required: true, message: 'Mailbox cannot be empty', trigger: 'blur' },
                        { type: 'email', message: 'Incorrect email format', trigger: 'blur' }
                    ],
                    gender: [
                        { required: true, message: 'Please select gender', trigger: 'change' }
                    ],
                    interest: [
                        { required: true, type: 'array', min: 1, message: 'Choose at least one hobby', trigger: 'change' },
                        { type: 'array', max: 2, message: 'Choose two hobbies at best', trigger: 'change' }
                    ],
                    desc: [
                        { required: true, message: 'Please enter a personal introduction', trigger: 'blur' },
                        { type: 'string', min: 20, message: 'Introduce no less than 20 words', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            handleSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.$Message.success('Success!');
                        // 信息有效且提交成功之后隐藏掉弹窗
                        this.$emit('listenChangeStr',false);//在响应该点击事件的函数中使用$emit来触发一个自定义事件，并传递一个参数
                    } else {
                        this.$Message.error('Fail!');
                    }
                })
            },
            handleReset (name) {
                this.$refs[name].resetFields();
            },
            feedbackClose: function (){
                this.$emit('listenChangeStr',false);//在响应该点击事件的函数中使用$emit来触发一个自定义事件，并传递一个参数

            }
        }
    }
</script>
<style>
.feedback{
    width: 550px;
    height: 450px;
    border-top: 1px solid #2C3E50;
    padding: 15px 30px 0;
    position: absolute;
    left: calc(50% - 280px);
    top: calc(50% - 230px);
    background: #fff;
    box-shadow: grey 5px 5px 50px -5px;
}
.formTitle{
    line-height: 30px;
    font-size: 16px;
    color: #2C3E50;
    margin-bottom: 10px;
    font-weight: bold;
    position: relative;
}
.feedback .ivu-radio-group{
    display: block;
}
.feedback .ivu-checkbox-group,.feedback .ivu-radio-group{
    text-align: left;
}
.formTitle span{
    position: absolute;
    right: -20px;
    top: -10px;
    cursor: pointer;
    z-index: 22;
}
.formTitle .feedbackClose{
    font-size: 20px;
}
</style>

